<?php
namespace app\admin\controller;

use app\admin\model\ProductInfo;
use think\Controller;
use think\Db;
use think\Session;
use app\admin\model\AdvisoryInfo;
class Prep extends Rbac
{
//    预科组管理页面
    public function prep()
    {
       $res = new AdvisoryInfo();
       $result = $res->where('job_group_id',0)->paginate(10);
       $this->assign('result',$result);
       return $this->fetch('prep/prep');
    }

//    预科组修改页面
    public function preupdate()
    {
        $job_id = request()->param('job_id');
        $res = new AdvisoryInfo();
        $result = $res->where('job_id',$job_id)->find()->toArray();
        $this->assign('result',$result);
        return $this->fetch('prep/preupdate');
    }
    //    预科组修改成功
    public function update()
    {
        $job_id = request()->param('job_id');
        $row = request()->param();
        $res = new AdvisoryInfo();
        $result = $res->allowField(true)->save($row,['job_id',$job_id]);
        if ($result){
            $this->success('修改成功','admin/prep/prep');
        }
    }

    //    预科组分组页面
    public function pregroup()
    {
        $job_id = input('job_id');
        $res = new AdvisoryInfo();
        $result =  $res->where('job_id',$job_id)->find();
        $this->assign('result',$result);
        return $this->fetch('prep/pregroup');
    }

    //    预科组批量分组页面
    public function pregroups()
    {
        $job_id = input('job_id');
//        var_dump($job_id);
        session::set('arrid',$job_id);
//        $getid =  session::get('arrid');
        $res = new AdvisoryInfo();
        $result =  $res->where('job_id',$job_id)->find();
        $this->assign('result',$result);
        return $this->fetch('prep/pregroups');
    }

//    预科组分组页面提交修改
    public function group(){
        $job_id = input('job_id');
        $data = request()->param('pro_id');
        $row = Db::table('mdm_job')->where('job_group_id',$data)->find();
//        if ($row['job_group_id']==$data){
//            $this->error('请不要重复分组');
//        }else{
            $info =[
                'job_group_id'=>$data
            ];
            $res = new AdvisoryInfo();
            $result= $res->save($info,['job_id'=>$job_id]);
            if ($result){
//                $this->success('分组成功');
                     echo "<script>parent.layer.msg('分组成功',{icon:1,time:2000});var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index); setTimeout(function () {main.location.href='admin/prep/prep';
window.parent.location.reload();},2000);</script>";
            }
    }
    //    预科组分组页面提交修改
    public function groups(){
        $getid =  session::get('arrid');
        $data = request()->param('pro_id');
        $row = Db::table('mdm_job')->where('job_group_id',$data)->select();
        if ($getid==''){
            $this->error('请选择分组学生');
        }
        for ($i = 0;$i<count($row);$i++){
            if ($data==$row[$i]['job_group_id']) {
                $data=$row[$i]['job_group_id'];
            }
        }
        $info =[
            'job_group_id'=>$data
        ];
        $res = new AdvisoryInfo();
        $result = $res->where('job_id in ('.$getid.')')->update($info);

        if ($result){
            echo "<script>parent.layer.msg('分组成功',{icon:1,time:2000});var index = parent.layer.getFrameIndex(window.name);parent.layer.close(index); setTimeout(function () {main.location.href='admin/prep/prep';
window.parent.location.reload();},2000);</script>";
//            echo "<script>location.reload();</script>";
        }
        session::destroy();
    }

//    分组ajax得到数据
    public function ajax(){
        $pro_sn = input('pro_sn');
        $res = new ProductInfo();
        $result = $res->where(['pro_sn'=>$pro_sn,'pro_date'=>2])->select();
        if ($result){
            echo json_encode(['msg'=>200,'result'=>$result]);
        }else{
            echo json_encode(['msg'=>300]);
        }
    }
    //    查看详情页面
    public function test(){
        $job_id = request()->param('job_id');
        $result =  Db::table('mdm_job')->alias('j')
                ->join('mdm_product p','j.job_group_id=p.pro_id')
                ->where('job_id',$job_id)
                ->find();
        $this->assign('result',$result);
        return $this->fetch('prep/test');
    }
}
